//index.js
//获取应用实例
var app = getApp()
var CommentUtils = app.CommonUtils;
var Keys = require('../../assets/data/keys.js')
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false, // loading
    userInfo: {},
    swiperCurrent: 0,
    selectCurrent: 0,
    categories: [],
    activeCategoryId: 0,
    goods: [],
    scrollTop: "0",
    loadingMoreHidden: true,
    hasNoCoupons: true,
    coupons: []
  },

  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  //事件处理函数
  swiperchange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

  /**
   * 跳转商品详情
   */
  toDetailsTap: function (e) {
    if (app.login_id.length > 0) {
      wx.navigateTo({
        url: "/pages/goods-details/index?id=" + this.data.goods[e.currentTarget.dataset.index].HJP_ID
      })
    } else {
      wx.showModal({
        title: '请登陆',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/index',
            })
          }
        }
      })
    }
  },
  tapBanner: function (e) {
    var temp_type = e.currentTarget.dataset.type;
    var temp_pid = e.currentTarget.dataset.pid;
    var temp_note = e.currentTarget.dataset.note;
    if (temp_type == "2") {
      //跳网页
    } else if (temp_type == "3") {
      if (app.login_id.length > 0) {
        wx.navigateTo({
          url: "/pages/goods-details/index?id=" + temp_pid
        })
      } else {
        wx.showModal({
          title: '请登陆',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../login/index',
              })
            }
          }
        })
      }
    }
  },


  // 3  shangpin  

  // 2 wangye 

  // 1 不能点

  onLoad: function () {
    wx.login({
      success: function (res) {
        console.log(res);
        wx.request({
          url: Keys.data.url_get_open_id,
          data: {
            DataType: "Xcx_Openid",
            code: res.code.toString(),
          },
          success: function (res) {
            if (res.data.isok == "true") {
              app.open_id = res.data.data.openid
            }
          }
        })
      }
    })

    var that = this
    wx.request({
      url: Keys.data.url_qingfeng,
      data: {
        DataType: "Home_AD",
      },
      success: function (res) {
        console.log("ddddddd");
        console.log(res);
        if (res.data.isok == "true") {
          that.setData({
            banners: res.data.datas
          });
        }
      },
    })
    that.getGoodsList();
  },
  getGoodsList: function () {
    var that = this;
    wx.request({
      url: Keys.data.url_home_product_list,
      success: function (res) {
        that.setData({
          goods: res.data.datas,
          loadingMoreHidden: true
        });
        var goods = [];
        if (res.data.code != 0 || res.data.datas.length == 0) {
          that.setData({
            loadingMoreHidden: false,
          });
          return;
        }
        for (var i = 0; i < res.data.datas.length; i++) {
          goods.push(res.data.datas[i]);
        }
        that.setData({
          goods: goods,
        });
      }
    })
    wx.request({
      url: Keys.data.url_home,
      data: {
        DataType: "Home_Advertise_List",
        "type": "微信分类",
      },
      success: function (res) {
        that.setData({
          goods_four: res.data.datas,
          loadingMoreHidden: true,
          hasNoCoupons: false,
        });
      }
    })
    wx.request({
      url: Keys.data.url_home,
      data: {
        DataType: "Home_Advertise_List",
        "type": "微信配送",
      },
      success: function (res) {
        that.setData({
          goods_peisong: res.data.datas,
          loadingMoreHidden: true
        });
      }
    })

  },
  gitCoupon: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    if (index == 0) {   //----------------------------------------平台介绍
      wx.navigateTo({
        url: '../introduce/index',
      })
    } else if (index == 1) {
      wx.navigateTo({
        url: '../happy-Drinking/index',
      })
    }
    else if (index == 2) {    //----------------------------------------优品推荐
      wx.navigateTo({
        url: '../recommend/index',
      })
    }
    else if (index == 3) {     //----------------------------------------我的酒窖
      if (app.login_id.length > 0) {
        wx.navigateTo({
          url: '../collect/index',
        })
      } else {
        wx.showModal({
          title: '请登陆',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../login/index',
              })
            }
          }
        })
      }
    }
  },

  /**
   * 搜索
   */
  btnSearch: function (e) {
    var _this = this;
    var temp_input = e.detail.value;

    wx.request({
      url: Keys.data.url_Product,
      data: {
        DataType: "Product_Query",
        name: temp_input,
      },
      success: function (res) {
        if (res.data.isok == "true") {
          if (res.data.datas.length > 0) {
            wx.setStorage({
              key: 'search',
              data: res.data.datas,
              success: function () {
                wx.navigateTo({
                  url: '../search/index',
                })
              }, fail: function () { CommentUtils.showToastWithImg("稍后尝试", 2) }
            })
          } else {
            CommentUtils.showToastWithImg("未找到商品", 3)
          }
        } else {
          CommentUtils.showToastWithImg("稍后尝试", 2)
        }
      }
    })
  }
  // onShareAppMessage: function () {
  //   return {
  //     title: wx.getStorageSync('mallName') + '——' + app.globalData.shareProfile,
  //     path: '/pages/index/index',
  //     success: function (res) {
  //       // 转发成功
  //     },
  //     fail: function (res) {
  //       // 转发失败
  //     }
  //   }
  // },
})

